package com.cljz.makepassenger.present.activity;

import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.GetCityBean;
import com.cljz.makepassenger.ui.activity.MapActivity;
import com.cljz.makepassenger.utils.L;

import java.util.HashMap;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.RequestBody;

/**
 * Created by apple on 2017/4/7.
 */

public class MapP extends BaseP<MapActivity> {
    public void getCity(String province, String city) {

        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("province", province);
        paramsMap.put("city", city);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        final RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getCityData(requestBody)
                .compose(XApi.<GetCityBean>getApiTransformer())
                .compose(XApi.<GetCityBean>getScheduler())
                .compose(getV().<GetCityBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<GetCityBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(GetCityBean getCityBean) {
                        L.e(getCityBean.getData().getCity_id());
                        L.e(getCityBean.getError_code());
                        if ("0".equals(getCityBean.getError_code())) {
                            getV().getCitySucceed(getCityBean);
                        } else if ("1000".equals(getCityBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getCityFail(getCityBean.getMsg());
                        }
                    }
                });

    }
}
